import os

import tornado.httpserver
import tornado.ioloop
import tornado.web

import my_uimodules
from handlers.home import HomeHandler
from handlers.page1 import Page1Handler
from handlers.page2 import Page2Handler


class PageNotFoundHandler(tornado.web.RequestHandler):
    def get(self):
        return self.write_error(404)

class Application(tornado.web.Application):
    def __init__(self):

        handlers = [
            (r"/", tornado.web.RedirectHandler, {"url": "/home"}),
            (r"/home", HomeHandler),

            (r"/page1", Page1Handler),
            (r"/page2", Page2Handler),

            (r".*", PageNotFoundHandler),
        ]

        settings = dict(
            static_path= os.path.join(os.path.dirname(__file__), "static"),
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            ui_modules=my_uimodules,
        )
        tornado.web.Application.__init__(self, handlers, **settings)


if __name__ == "__main__":
    port = 8899
    application = Application()
    http_server = tornado.httpserver.HTTPServer(application, xheaders=True)
    http_server.listen(port)

    print('Listen on http://localhost:{0}'.format(port))
    print('Listen on http://localhost:{0}/page1'.format(port))
    print('Listen on http://localhost:{0}/page2'.format(port))
    tornado.ioloop.IOLoop.instance().start()